<?php

namespace app\admin\controller;

class Index
{
    public function index()
    {
        $validate = validate_user();
        if($validate){
            return view('index/index');
        }else{
            return redirect("admin/index/login");
        }
    }

    public function login()
    {
        return view('login/login');
    }

    public function consumer()
    {
        $validate = validate_user();
        if($validate){
            return view('consumer/consumer');
        }else{
            return redirect("admin/index/login");
        }
    }

    public function loginout()
    {
        session(null);
        return redirect("admin/index/login");
    }

    /**
     * 登陆跳转
     */
    public function userLogin()
    {
        $request = request();
        if ($request->isPost()) {
            $username = $request->post("username");
            $userpass = $request->post("userpass");
            $remember = $request->post("remember");

            /*检查用户*/
            $adminModel = model("Admin");
            $res = $adminModel->field("id,username,userpass")->where("username", "=", $username)->find();
            if ($res) {
                if (md5($userpass."demo") == $res["userpass"]) {
                    session('admininfo.username', $username);
                    session('admininfo.uid', $res["id"]);
                    $data = [
                        "code" => 200,
                        "msg" => "",
                        "redirect" => "index.html",
                        "data" => "",
                    ];
                    return json($data);
                } else {
                    $data = [
                        "code" => 400,
                        "msg" => "用户账户或密码错误",
                        "redirect" => "",
                        "data" => "",
                    ];
                    return json($data);
                }
            } else {
                $data = [
                    "code" => 404,
                    "msg" => "用户账户不存在",
                    "redirect" => "",
                    "data" => "",
                ];
                return json($data);
            }
        }
    }

    /**
     * 修改密码
     */
    public function editPass()
    {
        $request = request();
        if ($request->isPost()) {
            $oldpass = $request->post("oldpass");
            $newpass = $request->post("newpass");
            $repass = $request->post("repass");
            $username=session("admininfo.username");
            $uid=session("admininfo.uid");
            /*检查用户*/
            $adminModel = model("Admin");
            $res = $adminModel->field("id,username,userpass")->where("username", "=", $username)->find();
            if ($res) {
                if (md5($oldpass."demo") == $res["userpass"]) {
                    if($newpass&&$repass&&$newpass==$repass){
                        $str=$newpass."demo";
                        $update=[
                            'userpass'=>md5($str)
                        ];

                        $res=$adminModel->where("id","=",$uid)->update($update);

                        if($res){
                            session(null);
                            $data = [
                                "code" => 200,
                                "msg" => "密码重置成功",
                                "redirect" => "login.html",
                                "data" => "",
                            ];
                            return json($data);
                        }else{
                            $data = [
                                "code" => 402,
                                "msg" => "密码重置失败",
                                "data" => "",
                            ];
                            return json($data);
                        }
                    }else{
                        $data = [
                            "code" => 405,
                            "msg" => "两次密码输入不一致",
                            "data" => "",
                        ];
                        return json($data);
                    }

                } else {
                    $data = [
                        "code" => 400,
                        "msg" => "原始密码错误",
                        "data" => "",
                    ];
                    return json($data);
                }
            } else {
                $this->error("访问错误");
            }
        }
    }

    /**
     * 首页统计数据
     * @return \think\response\Json
     */
    public function indexData(){
        $request = request();
        if ($request->isPost()) {
            $consumerModel=model("Consumer");
            /*获取当天数据*/
            $today=$consumerModel->whereTime("regTime","d")->count();
            /*获取当周数据*/
            $week=$consumerModel->whereTime("regTime","week")->count();
            /*获取当月数据*/
            $month=$consumerModel->whereTime("regTime","month")->count();

            $data=[
                "today"=>$today,
                "week"=>$week,
                "month"=>$month,
            ];
            $res=[
                "code"=>200,
                "msg"=>200,
                "data"=>$data,
            ];
            return json($res);
        }
    }

    /**
     * 获取客户数据
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function consumerInfo(){
        $request = request();
        if ($request->isPost()) {
            $page = $request->post("page");
            $pageSize = $request->post("pageSize");
            $startDate = $request->post("startDate");
            $endDate = $request->post("endDate");
            $province = $request->post("province");

            /*数据处理*/
            if(!$page){
                $page=1;
            }else{
                $page=(int)$page;
            }
            if(!$pageSize){
                $pageSize=50;
            }elseif ($pageSize<0){
                $pageSize=50;
            }elseif ($pageSize>50){
                $pageSize=50;
            }
            $consumerModel=model("Consumer");
            /*数据*/
            if($province){
                $condition=[
                    "province"=>$province
                ];
            }else{
                $condition="";
            }
            if($startDate){
                if($endDate){
                    $pageData=$consumerModel->where($condition)->whereTime("regTime","between",[$startDate,$endDate])->page($page,$pageSize)->select();
                    $totalSize=$consumerModel->where($condition)->whereTime("regTime","between",[$startDate,$endDate])->count();
                }else{
                    $pageData=$consumerModel->where($condition)->whereTime("regTime",">=",$startDate)->page($page,$pageSize)->select();
                    $totalSize=$consumerModel->where($condition)->whereTime("regTime",">=",$startDate)->count();
                }
            }else{
                if($endDate){
                    $pageData=$consumerModel->where($condition)->whereTime("regTime","<=",$endDate)->page($page,$pageSize)->select();
                    $totalSize=$consumerModel->where($condition)->whereTime("regTime","<=",$endDate)->count();
                }else{
                    $pageData=$consumerModel->where($condition)->page($page,$pageSize)->select();
                    $totalSize=$consumerModel->where($condition)->page($page,$pageSize)->count();
                }
            }
            $res=[
                "code"=>200,
                "msg"=>"获取成功",
                "data"=>$pageData,
                "totalSize"=>$totalSize,
                "page"=>$page,
                "pageSize"=>$pageSize,
            ];
            return json($res);
        }
    }
}
